package de.bensoft.jrail.client.ui.trackplan.segments;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Side;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.MenuItem;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.stage.Modality;
import javafx.stage.Stage;

import javax.inject.Inject;

import de.bensoft.jrail.business.entity.DeflectorData;
import de.bensoft.jrail.business.entity.TrackplanSegment;
import de.bensoft.jrail.client.com.DeflectorComunicator;
import de.bensoft.jrail.client.com.DeflectorInstance;
import de.bensoft.jrail.client.common.FormCreator;
import de.bensoft.jrail.client.common.SceneCreatorResult;
import de.bensoft.jrail.client.ui.trackplan.TrackplanController;
import de.bensoft.jrail.client.ui.trackplan.TrackplanController.Tool;
import de.bensoft.jrail.client.ui.trackplan.deflectoradmin.DeflectorAdminController;


public class DeflectorTrackSegment extends ActionTrackSegment {

	@Inject
	FormCreator formCreator;
	
	@Inject
	DeflectorComunicator deflCommunicator;
	
	@Inject
	TrackplanController tpController;
	
	private DeflectorInstance deflectorInstance;	
	public  DeflectorInstance getDeflectorInstance() { return deflectorInstance; }
	public void setDeflectorInstance(DeflectorInstance deflectorInstance) { this.deflectorInstance = deflectorInstance;  }

	private ContextMenu contextMenu;
	
	public DeflectorTrackSegment() {
		super();
			
		// Contextmenu
		contextMenu = new ContextMenu();
		MenuItem mi1 = new MenuItem("Configure");
		contextMenu.getItems().addAll(mi1);
		
		
		mi1.setOnAction(new EventHandler<ActionEvent>() {
			public void handle(ActionEvent arg0) {
				showDeflectorAdmin();
			}
		});
	}
	
	@Override
	public void onClicked(MouseEvent event) {
		super.onClicked(event);
			
		if(event.getButton().equals(MouseButton.SECONDARY)) {
			contextMenu.show(this, Side.RIGHT, 0, 0);
		} else if(event.getButton().equals(MouseButton.PRIMARY)) {
			onLeftClick();
		}
	}
	
	private void onLeftClick() {
		if(tpController.getCurrentTool().equals(Tool.SELECT)) {
			deflectorInstance.straightProperty().set(!deflectorInstance.straightProperty().get());
		}
	}
	
	private void obtainInstance() {
		deflectorInstance = deflCommunicator.getInstanceByData(getSegmentData());	
		updateIcon();
		deflectorInstance.straightProperty().addListener(new ChangeListener<Boolean>() {
			public void changed(ObservableValue<? extends Boolean> arg0, Boolean arg1, Boolean arg2) {
				updateIcon();
			}
		});
	}

	private void showDeflectorAdmin() {
		
		SceneCreatorResult res = formCreator.createScene(DeflectorAdminController.class);
		DeflectorAdminController controller = (DeflectorAdminController)res.getController();
			
		controller.setDeflectorSegment(this);
		
		Stage stage = new Stage();
		stage.setScene(res.getScene());
		stage.setTitle("DeflectorAdmin");
		stage.setResizable(false);
		stage.sizeToScene();
		stage.initModality(Modality.APPLICATION_MODAL);
		stage.show();
	}
	
	private void provideDeflectorData() {
		if(getSegmentData().getDeflectorData() == null) {
			DeflectorData d = new DeflectorData();
			d.setInvert   (false);
			d.setStraight (true);
			d.setAddress(0l);
			
			getSegmentData().setDeflectorData(d);
		}
		
		obtainInstance();
	}
	
	@Override
	public void setSegmentData(TrackplanSegment segmentData) {
		super.setSegmentData(segmentData);
		provideDeflectorData();
	}
	
	
	String iconCurved;
	public void setIconCurved(String icon) {
		iconCurved = icon;
	}
	
	private void updateIcon() {
		
		if(deflectorInstance.straightProperty().get())
			setGraphic(getIconURL());
		else
			setGraphic(iconCurved);
	}
	
}
